<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择性别" clearable>
          <el-option
            v-for="dict in dict.type.sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="出生日期" prop="birth">
        <el-date-picker
          clearable
          v-model="queryParams.birth"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择出生日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="联系方式" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="人才类型" prop="eliteType">
        <el-select v-model="queryParams.eliteType" placeholder="请选择人才类型" clearable>
          <el-option
            v-for="dict in dict.type.elite_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审批备注" prop="advice">
        <el-input
          v-model="queryParams.advice"
          placeholder="请输入审批备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:certification:export']"
        >
          导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="certificationList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="索引" align="center" prop="certificationId" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex" />
        </template>
      </el-table-column>
      <el-table-column label="出生日期" align="center" prop="birth" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birth, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="联系方式" align="center" prop="phonenumber" />
      <el-table-column label="身份证号" align="center" prop="idCard" />
      <el-table-column label="人才类型" align="center" prop="eliteType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.elite_type" :value="scope.row.eliteType" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="证明材料" align="center" prop="certificationMaterial" /> -->
      <el-table-column label="审批状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.approval_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="审批备注" align="center" prop="advice" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(scope.row, 2)"
            v-hasPermi="['system:certification:edit']"
          >
            查看
          </el-button>
          <el-button
            v-if="scope.row.status == 0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row, 3)"
            v-hasPermi="['system:certification:edit']"
          >
            审核
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改人才认证申请对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" :disabled="openType == 2">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" disabled />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别" disabled>
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出生日期" prop="birth">
          <el-date-picker
            clearable
            v-model="form.birth"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择出生日期"
            disabled
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="联系方式" prop="phonenumber">
          <el-input v-model="form.phonenumber" placeholder="请输入联系方式" disabled />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证号" disabled />
        </el-form-item>
        <el-form-item label="人才类型" prop="eliteType">
          <el-select v-model="form.eliteType" placeholder="请选择人才类型" disabled>
            <el-option
              v-for="dict in dict.type.elite_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="证明材料" prop="certificationMaterial">
          <file-upload v-model="form.certificationMaterial" :dis="true" :showTip="openType != 2" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" disabled />
        </el-form-item>
        <el-form-item label="是否通过" prop="approvalStatus" v-if="openType != 2">
          <el-select
            v-model="form.approvalStatus"
            placeholder="请选择是否通过"
            :disabled="openType == 2"
          >
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审批备注" prop="advice">
          <el-input v-model="form.advice" placeholder="请输入审批备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCertification,
  getCertification,
  delCertification,
  addCertification,
  updateCertification,
  approvalCertification
} from '@/api/system/certification'

export default {
  name: 'Certification',
  dicts: ['elite_type', 'approval_status', 'sys_yes_no', 'sys_user_sex'],
  data() {
    return {
      openType: 1,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人才认证申请表格数据
      certificationList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        sex: null,
        birth: null,
        phonenumber: null,
        idCard: null,
        eliteType: null,
        certificationMaterial: null,
        status: null,
        advice: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        advice: [{ required: true, message: '请输入审批备注', trigger: 'blur' }],
        approvalStatus: [{ required: true, message: '请选择是否通过', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询人才认证申请列表 */
    getList() {
      this.loading = true
      listCertification(this.queryParams).then((response) => {
        this.certificationList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        certificationId: null,
        name: null,
        sex: null,
        birth: null,
        phonenumber: null,
        idCard: null,
        eliteType: null,
        certificationMaterial: null,
        status: null,
        advice: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        approvalStatus: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.certificationId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加人才认证申请'
    },
    /** 修改按钮操作 */
    handleUpdate(row, openType) {
      this.openType = openType
      this.reset()
      const certificationId = row.certificationId || this.ids
      getCertification(certificationId).then((response) => {
        this.form = response.data
        const urls = JSON.parse(response.data.certificationMaterial)
        this.form.certificationMaterial = urls.map((element) => {
          return element.fileName
        })
        this.open = true
        this.title = openType == 2 ? '查看人才认证申请' : '审批人才认证申请'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const params = {
            certificationId: this.form.certificationId,
            approvalStatus: this.form.approvalStatus,
            advice: this.form.advice
          }
          if (this.form.certificationId != null) {
            approvalCertification(params).then((response) => {
              this.$modal.msgSuccess('审批成功')
              this.open = false
              this.getList()
            })
          } else {
            addCertification(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const certificationIds = row.certificationId || this.ids
      this.$modal
        .confirm('是否确认删除人才认证申请编号为"' + certificationIds + '"的数据项？')
        .then(function () {
          return delCertification(certificationIds)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'system/certification/export',
        {
          ...this.queryParams
        },
        `certification_${new Date().getTime()}.xlsx`
      )
    }
  }
}
</script>
